﻿using System;
using System.Windows.Forms;
using ReagentStore.Properties;
using ReagentStore.UTL;

namespace ReagentStore
{
	public partial class UserForm : Form
	{
		private readonly FormValidator _formValidator;

		public UserForm()
		{
			InitializeComponent();
			_formValidator = new FormValidator(errorProvider);
			_formValidator.Add(new RequiredValidator(textUser, "请输入单位名称"));
		}

		private void UserForm_Load(object sender, EventArgs e)
		{
			textUser.Text = Settings.Default.Company;
		}

		private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			const string userInit = "请输入单位名称";
			string user = textUser.Text;

			if (DialogResult != DialogResult.OK)
				return;

			errorProvider.SetError(textUser, string.Empty);
			if (!_formValidator.IsValid()
			    || (user.Equals(userInit)))
			{
				errorProvider.SetError(textUser, "请输入单位名称");
				e.Cancel = true;
				return;
			}

			Settings.Default.Company = user;
			Settings.Default.Save();
		}
	}
}